/* eslint valid-jsdoc: "off" */
const path = require('path');
/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1760848864580_7037';

  // add your middleware config here
  config.middleware = [];

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };
  config.security = {
    csrf: {
      enable: false,
    },
    domainWhiteList: ['http://localhost:5173'],
  }
  config.cors = {
    origin: '*',
    credentials: true,
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
  }
  config.jwt = {
    secret: '123456',
    expiresIn: '7d',
  }
  config.sequelize = {
    dialect: 'mysql',
    database: 'jmu_db',
    host: 'localhost',
    port: 3307,
    username: 'root',
    password: 'root',
    timezone: '+08:00',
    dialectOptions: {
      dateStrings: true,
      typeCast: true
    }
  }
  // 静态文件配置
  config.static = {
    prefix: '/public/',
    dir: [
      path.join(appInfo.baseDir, 'app/public'),
      path.join(appInfo.baseDir, 'app/public/upload/avatar'),
      path.join(appInfo.baseDir, 'app/public/upload/post'),
      path.join(appInfo.baseDir, 'app/public/upload/circle_post')
    ],
    dynamic: true,
    preload: false,
    maxAge: 31536000,
  };

  // 文件上传配置
  config.multipart = {
    mode: 'file',
    fileSize: '10mb',
    fileExtensions: ['.jpg', '.jpeg', '.png', '.gif', '.webp'],
    whitelist: ['.jpg', '.jpeg', '.png', '.gif', '.webp'],
    tmpdir: path.join(appInfo.baseDir, 'app/public/tmp'),
  };

  config.upload = {
    // 本地存储目录配置
    avatarDir: path.join(appInfo.baseDir, 'app/public/upload/avatar'), // 头像存储目录
    postImageDir: path.join(appInfo.baseDir, 'app/public/upload/post'), // 帖子图片存储目录
    circlePostImageDir: path.join(appInfo.baseDir, 'app/public/upload/circle_post'), // 圈子帖子图片存储目录

    // 完整访问URL配置
    avatarBaseUrl: 'http://localhost:7001/public/upload/avatar/', // 头像访问URL
    postImageBaseUrl: 'http://localhost:7001/public/upload/post/', // 帖子图片访问URL
    circlePostImageBaseUrl: 'http://localhost:7001/public/upload/circle_post/', // 圈子帖子图片访问URL

    resumeImageDir: path.join(appInfo.baseDir, 'app/public/resumes'), // 存储目录
    resumeImageBaseUrl: '/public/resumes/', // 访问URL前缀
    // 添加商品图片配置
    goodImageDir: path.join('app/public/uploads/goods'),
    goodImageBaseUrl: '/public/uploads/goods/',

    resumeImageDir: path.join('app/public/uploads/resumes'), // 简历图片存储目录
    resumeImageBaseUrl: 'http://localhost:7001/public/uploads/resumes/', // 简历图片访问基础URL
  };

  config.static = {
    prefix: '/public/', // 静态资源访问前缀
    dir: path.join(appInfo.baseDir, 'app/public'), // 静态资源存储目录
  };

  // Socket.IO 配置
  config.io = {
    init: {}, // passed to engine.io
    namespace: {
      '/': {
        connectionMiddleware: ['auth'],
        packetMiddleware: [],
      },
    },
    // Redis 配置是可选的,单进程应用不需要
    // 如果需要多进程支持,请安装并启动 Redis,然后取消下面的注释
    // redis: {
    //   host: '127.0.0.1',
    //   port: 6379
    // }
  };

  // config.upload = {
  //   avatarDir: path.join(appInfo.baseDir, 'app/public/uploads/avatars'),
  //   avatarBaseUrl: '/public/uploads/avatars/',
  //   postImageDir: path.join(appInfo.baseDir, 'app/public/uploads/posts'),
  //   postImageBaseUrl: '/public/uploads/posts/',
  //   circlePostImageDir: path.join(appInfo.baseDir, 'app/public/uploads/circle-posts'),
  //   circlePostImageBaseUrl: '/public/uploads/circle-posts/',
  //   // 添加商品图片配置
  //   goodImageDir: path.join(appInfo.baseDir, 'app/public/uploads/goods'),
  //   goodImageBaseUrl: '/public/uploads/goods/'
  // };
  config.session = {
    key: 'EGG_SESS',
    maxAge: 24 * 3600 * 1000, // 1天
    httpOnly: true,
    encrypt: true,
    renew: true,
    sameSite: 'lax',
    signed: true,
    // 添加以下配置
    domain: 'localhost', // 确保前后端在同一域名下
    path: '/',
  };
  return {
    ...config,
    ...userConfig,
  };
};